<template>
  <rx-layout>
    <div slot="center" style>
      <rx-fit>
        <div slot="toolheader" foldheader="true" foldbtn="false" border="false">
          <div class="table-operator">
            <rx-button alias="sysJobTaskAdd" :butn-icon="'plus'" :butn-type="'primary'" :show-no-right="true" @click="add">{{$ti18('rxList.addBtn','新建')}}</rx-button>
            <a-dropdown v-if="editEnable && selectedRowKeys.length > 0 || removeEnable && selectedRowKeys.length > 0">
              <a-button style="margin-left: 8px"> {{$ti18('rxList.moreBtn','更多')}} <a-icon type="down" /> </a-button>
              <a-menu slot="overlay">
                <a-menu-item key="1" v-if="editEnable && selectedRowKeys.length > 0" alias="sysJobTaskEdit" :butn-icon="'edit'"
                             :butn-type="'primary'" :show-no-right="false" @click="editOne"><a-icon type="edit"  />{{$ti18('rxList.editBtn','编辑')}}</a-menu-item>
                <a-menu-item key="2" v-if="removeEnable && selectedRowKeys.length > 0" alias="sysJobTaskDelete" :butn-icon="'delete'" :butn-type="'danger'"
                             :show-no-right="false" @click="delByIds(selectedRowKeys)"><a-icon type="delete" />{{$ti18('rxList.delBtn','删除')}}</a-menu-item>
              </a-menu>
            </a-dropdown>
          </div>
          <span class="search-btn-box">
                <span class="search-btn" @click="searchshow"><i class="iconfont iconguolv"></i>{{$ti18('rxList.leachBtn','过滤')}}</span>
           </span>
        </div>
        <div slot="searchbar" btnalign="right"  v-if="fitSearch" @search="search" @cancel="cancel" @close="close">
          <a-form layout="vertical">
                <a-form-item :label="i18n('form.name','名称')">
                    <a-input :placeholder="i18n('form.name','名称','input')" v-model="queryParam.Q_NAME__S_LK"/>
                </a-form-item>
                <a-form-item :label="i18n('form.status','状态')">
<!--                    <a-input :placeholder="i18n('form.status','状态','input')" v-model="queryParam.Q_STATUS__S_LK"/>-->
                    <a-select style="width: 100%" :placeholder="i18n('form.status','请选择状态','select')"
                              v-model="queryParam.Q_STATUS__S_LK">
                        <a-select-option value="1">{{i18n('form.enable','启用')}}</a-select-option>
                        <a-select-option value="0">{{i18n('form.disable','停用')}}</a-select-option>
                    </a-select>
                </a-form-item>
                <a-form-item :label="i18n('form.typeDesc','类型')">
                    <a-input :placeholder="i18n('form.type','类型','input') " v-model="queryParam.Q_TYPE__S_LK"/>
                </a-form-item>
          </a-form>
        </div>
        <rx-grid
                ref="table"
                :allowRowSelect="true"
                :multiSelect="true"
                :columns="columns"
                :bordered="isBorder"
                :defaultPageSize="10"
                url="/api-system/system/core/sysJobTask/query"
                :queryParam="queryParam"
                data-field="result.data"
                :pageSizeOptions="['10','30','40']"
                id-field="id"
                @selectChange="onSelectChange"
        >
            <span slot="status" slot-scope="{ text, record }">
              <a-tag :color="statusMap[text].color">
              <span>{{statusMap[text].text}}</span>
              </a-tag>
            </span>
            <span slot="appId" slot-scope="{ text, record }">
                {{allApps[text]?$i18(allApps[text].appName,'应用名称'):$ti18('rxList.noAppName','无')}}
            </span>

            <span slot="type" slot-scope="{ text, record }">
              <a-tag :color="typeMap[text].color">
              <span>{{typeMap[text].text}}</span>
              </a-tag>
            </span>
             <span slot="action" slot-scope="{ text, record }" class="actionIcons">
                 <span v-if="editEnable " @click="edit(record)">{{$ti18('rxList.editBtn','编辑')}}</span>
                 <span v-if="removeEnable " @click="delByIds([record.id])">{{$ti18('rxList.delBtn','删除')}}</span>
                 <span  @click="scheduleTask(record)">{{i18n('list.scheduling','调度')}}</span>
             </span>
        </rx-grid>
      </rx-fit>
    </div>
  </rx-layout>
</template>

<script>
  import SysJobTaskApi from '@/api/system/core/sysJobTask'
  import SysJobTaskEdit from './SysJobTaskEdit'
  import {mapState} from 'vuex';
  import SysJobEdit from "@/views/modules/system/core/SysJobEdit";
  import {BaseList,RxFit,RxGrid,Util} from "@lowcode/jpaas-common-lib";

  export default {
    name: 'SysJobTaskList',
    mixins: [BaseList],
    components: {
      RxFit
    },
    computed: {
        ...mapState({
            // 所属应用
            allApps: state => state.appSetting ? state.appSetting.allApps : {}
        }),
    },
    created(){

      if (this.curApp && this.curApp.appId){
          this.needFilterAppId();
      }else {
          let appObj= {title: this.i18n('form.appId', '所属应用'), dataIndex: 'appId', width: 80,scopedSlots: {customRender: 'appId'}}
          this.columns.splice(4,0,appObj);
      }
    },
    data() {
      return {
        columns: [
          {
            title: this.$ti18('rxList.index','序号'),
            type: 'indexColumn',
            width: 60,
            dataIndex: 'serial',
            scopedSlots: {customRender: 'serial'}
          },
          {title: this.i18n('form.name','名称'), dataIndex: 'name', width: 100},
          {title: this.i18n('form.status','状态'), dataIndex: 'status', width: 80,scopedSlots: {customRender: 'status'}},
          {title: this.i18n('form.type','类型'), dataIndex: 'type', width: 80,scopedSlots: {customRender: 'type'}},
          //{title: this.i18n('form.appId', '所属应用'), dataIndex: 'appId', width: 120,scopedSlots: {customRender: 'appId'}},
          {title: this.$ti18('rxForm.createTime','创建时间'), dataIndex: 'createTime', sorter: true, sortField: 'CREATE_TIME_', width: 120},
          {title: this.$ti18('rxList.action','操作'), width: 80, dataIndex: 'action', scopedSlots: {customRender: 'action'}}
        ],
        component:SysJobTaskEdit,
        queryParam:{},
        comment: this.i18n('list.comment','定时任务定义'),
        widthHeight:['700px','600px'],
        fitSearch:false,
          statusMap: {
              "1": {color: 'green', text: this.i18n('form.enable','启用')},
              "0": {color: 'red', text: this.i18n('form.disable','停用')},
          },
          typeMap: {
              "sql": {color: 'green', text: 'SQL'},
              "script": {color: 'red', text: this.i18n('form.script','脚本')},
              "class": {color: 'blue', text: this.i18n('form.timeClass','定时处理类')},
          },
      }
    },

    methods:{
      i18n(name,text,key){
          return this.$ti18(name,text,"SysJobTask",key);
      },
      delById(ids){
        return SysJobTaskApi.del({ ids: ids.join(',') });
      },
      getListByParams(parameter){
        return SysJobTaskApi.query(parameter)
      },
      scheduleTask(record){
          Util.open({
              component:SysJobEdit,
              curVm:this,
              widthHeight:['800px','600px'],
              title: this.i18n('task.title','任务调度'),
              data:{
                  jobTask:{
                      jobTaskId:record.id,
                      jobTask:record.name
                  }
              }
          },function (action){});
      }

    }
  }
</script>
<style scoped>

</style>

